package com.yunnuo.monitor.job.service.impl;

import com.yunnuo.monitor.job.dao.IJobDao;
import com.yunnuo.monitor.job.entity.MonJob;
import com.yunnuo.monitor.job.service.IJobService;
import com.yunnuo.system.comm.util.Empty;
import com.yunnuo.system.comm.util.page.TableDataInfo;
import com.yunnuo.system.users.entity.PageParams;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import javax.persistence.criteria.*;
import java.util.Date;
import java.util.List;

/**
 * @author 魏洪福（15064000912@163.com）
 * @create 2018-09-25-13:43
 */
@Service
public class JobServiceImpl implements IJobService {



    @Autowired
    private IJobDao jobDao;

    @Override
    public void addVO(MonJob vo) throws ServiceException {
        this.jobDao.saveAndFlush(vo);
    }

    @Override
    public void updateVO(MonJob vo) throws ServiceException {
          this.jobDao.saveAndFlush(vo);
    }

    @Override
    public void delList(String[] ids) throws ServiceException {

    }

    @Override
    public void deleteVo(String id) throws ServiceException {
        this.jobDao.delete(id);
    }

    @Override
    public List<MonJob> findList() throws ServiceException {
        return this.jobDao.findAll();
    }

    @Override
    public List<?> findList(MonJob vo, String other) throws ServiceException {
        return this.jobDao.findAll();
    }

    @Override
    public MonJob findVO(String id) throws ServiceException {
        return this.jobDao.findOne(id);
    }

    /**
     * 分页查询
     * @param pageParams
     * @param pageRequest
     * @return
     */
    @Override
    public TableDataInfo selecJobByPageParams(PageParams pageParams, PageRequest pageRequest) {
        TableDataInfo dataInfo=new TableDataInfo();
        Specification<MonJob> specification=new Specification<MonJob>() {
            @Override
            public Predicate toPredicate(Root<MonJob> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> namePath=root.get("taskName");
                String value=pageParams.getSearchValue();

                if(Empty.isNotEmpty(value)){
                    query.where(cb.like(namePath,"%"+value+"%"));
                }
                return null;
            }
        };
        Page<MonJob> dictTypes=jobDao.findAll(specification,pageRequest);
        dataInfo.setRows(dictTypes.getContent());
        dataInfo.setTotal(dictTypes.getTotalElements());
        return dataInfo;
    }

    /**
     * 保存或修改
     * @param job
     */
    @Override
    public MonJob save(MonJob job)  {
        //完善信息
        if(null!=job.getId()&&!"".equals(job.getId())){
            job.setUpdateTime(new Date());
        }else{
            job.setCreateTime(new Date());
        }
        MonJob newJob=jobDao.save(job);
        return newJob;
    }

}
